package com.yuanxin.chooseimage;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class PermissionActivity extends AppCompatActivity {
    private static final int REQUEST_CODE = 0x001;//请求码
    private String[] needPersition = new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private List<String> denyPermissions = new ArrayList<>();//记录没有通过的权限

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findDeniedPermissions(PermissionActivity.this, needPersition);
        if(denyPermissions.size() > 0){
            //弹出申请权限提示框
            ActivityCompat.requestPermissions(PermissionActivity.this,
                    denyPermissions.toArray(new String[denyPermissions.size()]),//将集合转换为数组格式
                    REQUEST_CODE);
            Log.e("TAG","弹出申请权限提示框");
        }else{
            Log.e("TAG","弹出申请权限提示框11111111111");
            setResult(0);
            finish();
        }

        Log.e("else", "申请权限");
    }


    @TargetApi(Build.VERSION_CODES.M)
    public List<String> findDeniedPermissions(Activity activity, String... permission) {
        //遍历所需的权限
        for (String value : permission) {
            //检查权限是否通过，如果没有通过添加到集合中
            if (activity.checkSelfPermission(value) != PackageManager.PERMISSION_GRANTED) {
                denyPermissions.add(value);
                Log.e("TAG","5555555555555555555");
            }
        }
        return denyPermissions;
    }
    //
    //申请权限回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length == permissions.length) {
                denyPermissions.clear();//清除集合
                for (int i = 0; i < grantResults.length; i++) {
                    //如果用户拒绝了权限申请，添加到集合中
                    if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                        denyPermissions.add(permissions[i]);
                        Log.e("TAG","66666666666666666666");
                    }
                }
                //如果集合中有数据则表示有权限申请未通过
                if (denyPermissions.size() > 0) {
                    //再次请求或者其他操作
                    ActivityCompat.requestPermissions(PermissionActivity.this,
                            denyPermissions.toArray(new String[denyPermissions.size()]),
                            REQUEST_CODE);
                    Toast.makeText(this, "有权限没有通过", Toast.LENGTH_LONG).show();
                    //打印未通过的权限
                    Log.e("TAG","777777777777777777");
                    Log.e("defined...", denyPermissions.toString());
                }else{
                    //没有未通过的权限
                    Log.e("TAG","888888888888888888");
                    finish();
                }
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
